<?php

namespace App\Http\Controllers\VideoAdmin;

use App\Http\Config\ErrorCode;
use App\Http\Controllers\Admin\BaseController;
use App\Models\video\ServiceModel;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class ServiceController extends BaseController
{
    private $field = [
        "name" => "require", "icon_id" => "require", "sort" => 0
    ]; //填入数据库需要存储的表字段
    private $fieldMsg = ["name" => "名称", "icon_id" => "图标"];
    private $data = [];

    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    public function getList(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->return_result($this->returnData);
        }
        $page = $request->page ?: 1;
        $pageSize = $request->pageSize ?: 10;
        $data = ServiceModel::getList("*", ["mall_id" => $request->mallData["id"]], $page, $pageSize);
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    public function getAll(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $data = ServiceModel::getFields("id,name", ["mall_id" => $request->mallData["id"]], false, false);
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    // 添加
    public function create(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        foreach ($this->field as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if($v === 'require' && !$request->$ks){
                return ErrorCode::code("params_not_exist", $this->fieldMsg[$k]);
            }
            $this->data[$k] =  $request->$ks ? trim($request->$ks) : $v;
        }
        ServiceModel::toAdd($this->data);
        return $this->return_result($this->returnData);
    }

    // 更新
    public function updateInfo(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        foreach ($this->field as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if($v === 'require' && !$request->$ks){
                return ErrorCode::code("params_not_exist", $this->fieldMsg[$k]);
            }
            if($request->$ks) {
                $this->data[$k] = trim($request->$ks);
            }
        }
        ServiceModel::doUpdate($this->data, ["id" => $request->id]);
        return $this->return_result($this->returnData);
    }

    // 获取信息
    public function getInfo(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $data = ServiceModel::getFields("*", ["id" => $request->id]);
        if(!empty($data)){
            $this->returnData["data"] = $data;
        }else{
            $this->returnData["data"] = [];
        }
        return $this->return_result($this->returnData);
    }

    // 删除
    public function delete(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        ServiceModel::delById($request->id);
        return $this->returnData;
    }
}
